def length_of_longest_substring(s: str) -> int:
    res = 0  # 保存最长字串长度
    window = ""  # 字符串实现滑动窗口
    for i in s:
        if i in window:
            window = window[window.index(i) + 1:] + i  # 更新滑动窗口
        else:
            window += i
            res = max(res, len(window))  # 更新最长字串长度
    return res


def length_of_longest_substring2(s: str) -> int:
    """双指针实现滑动窗口"""
    res = 0  # 保存最长子串长度
    lp, rp = 0, 0  # 双指针实现滑动窗口
    for i in range(len(s)):
        if s[i] in s[lp: rp]:
            lp += s[lp: rp].index(s[i]) + 1  # 更新滑动窗口
            rp += 1
        else:
            rp += 1
            res = max(res, rp - lp)  # 更新最长子串长度最大值
    return res


def length_of_longest_substring3(s: str) -> int:
    res = 0
    window = {}  # 字典实现滑动窗口，优化查找速度
    left = -1  # 窗口的左边界
    for i, c in enumerate(s):
        if c in window and window[c] > left:  # 更新滑动窗口
            left = window[c]
            window[c] = i
        else:
            window[c] = i
            res = max(res, i - left)
    return res


if __name__ == '__main__':
    print(length_of_longest_substring2(" "))
